package com.example.sicnuaccountingapp.ui.profile

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.fragment.app.Fragment
import androidx.fragment.app.viewModels
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.repeatOnLifecycle
import androidx.navigation.fragment.findNavController
import com.example.sicnuaccountingapp.R
import com.example.sicnuaccountingapp.databinding.FragmentProfileBinding
import com.example.sicnuaccountingapp.utils.PreferencesManager
import com.example.sicnuaccountingapp.data.entity.User
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.launch
import javax.inject.Inject

@AndroidEntryPoint
class ProfileFragment : Fragment() {
    private var _binding: FragmentProfileBinding? = null
    private val binding get() = _binding!!

    private val viewModel: ProfileViewModel by viewModels()

    @Inject
    lateinit var preferencesManager: PreferencesManager

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        _binding = FragmentProfileBinding.inflate(inflater, container, false)
        return binding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        setupViews()
        observeState()
    }

    private fun setupViews() {
        binding.apply {
            editProfileButton.setOnClickListener {
                showEditProfileDialog()
            }
            changePasswordButton.setOnClickListener {
                showChangePasswordDialog()
            }
            categoryManagementButton.setOnClickListener {
                findNavController().navigate(R.id.action_profile_to_categoryManagement)
            }
            logoutButton.setOnClickListener {
                viewModel.logout()
            }
        }
    }

    private fun observeState() {
        viewLifecycleOwner.lifecycleScope.launch {
            viewLifecycleOwner.repeatOnLifecycle(Lifecycle.State.STARTED) {
                viewModel.state.collect { state ->
                    when (state) {
                        is ProfileUiState.Success -> {
                            updateUserInfo(state.user)
                        }
                        is ProfileUiState.Error -> {
                            Toast.makeText(context, state.message, Toast.LENGTH_SHORT).show()
                        }
                        ProfileUiState.Loading -> {
                            // 可以显示加载状态
                        }
                    }
                }
            }
        }
    }

    private fun updateUserInfo(user: User) {
        binding.apply {
            usernameText.text = user.username
            emailText.text = user.email ?: getString(R.string.not_set)
            phoneText.text = user.phone ?: getString(R.string.not_set)
        }
    }

    private fun showEditProfileDialog() {
        EditProfileDialog.newInstance()
            .show(childFragmentManager, EditProfileDialog.TAG)
    }

    private fun showChangePasswordDialog() {
        ChangePasswordDialog.newInstance()
            .show(childFragmentManager, ChangePasswordDialog.TAG)
    }

    override fun onDestroyView() {
        super.onDestroyView()
        _binding = null
    }
} 